package net.goutalk.glcs.module.organization.controller;


import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.utils.RedisUtil;
import net.goutalk.glcs.module.organization.dto.WeChatDepartPageDto;
import net.goutalk.glcs.module.organization.dto.WeChatPageDto;
import net.goutalk.glcs.module.organization.entity.Department;
import net.goutalk.glcs.module.organization.entity.User;
import net.goutalk.glcs.module.organization.service.DingtalkService;
import net.goutalk.glcs.module.organization.service.IDepartmentService;
import net.goutalk.glcs.module.organization.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/ding-talk")
@Api(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/ding-talk", tags = "钉钉信息")
@AllArgsConstructor
public class DingtalkController {

    private final IUserService userService;

    private final IDepartmentService departmentService;


    private DingtalkService dingtalkService;

    private RedisUtil redisUtil;

    @PutMapping("/sync-user")
    @ApiOperation(value="钉钉信息更新")
    @GlcsLog(value = "钉钉用户部门同步更新")
    public R updateInfo() throws Exception{
        dingtalkService.updateInfo();
        CompletableFuture.runAsync(() -> {
            List<Department> depList = departmentService.list();
            redisUtil.set(GlobalConstant.DEP_CACHE_KEY, depList);

            List<User> userList = userService.list();
            redisUtil.set(GlobalConstant.USER_CACHE_KEY, userList);
        });
        return  R.ok(true);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="获取员工分页信息")
    @GlcsLog(value = "获取员工分页信息")
    public R page(WeChatPageDto dto){
        return  R.ok(userService.getPage(dto));
    }

    @GetMapping(value = "/departpage")
    @ApiOperation(value="获取部门分页信息")
    @GlcsLog(value = "获取部门分页信息")
    public R page(WeChatDepartPageDto dto){
        return  R.ok(departmentService.Ipage(dto));
    }
}
